package com.guli.sms.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

import com.guli.common.valid.SaveGroup;
import com.guli.sms.vo.PromotionAndPromotionSession;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.guli.sms.entity.SeckillPromotionEntity;
import com.guli.sms.service.SeckillPromotionService;

import com.guli.common.utils.R;


/**
 * 秒杀活动
 *
 * @author LuoJunHua
 * @email 1007052116@qq.com
 * @date 2020-11-29 10:59:49
 */
@RestController
@RequestMapping("/sms/seckillpromotion")
public class SeckillPromotionController {

    @Autowired
    private SeckillPromotionService seckillPromotionService;

    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取seckillPromotion分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size, @RequestParam(required = false) Long promotionId) {

        Page<SeckillPromotionEntity> page = seckillPromotionService.queryPage(currentPage, size, promotionId);

        return R.ok().put("page", page);
    }

    @GetMapping("/getListBySecKillSessionId/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取seckillPromotion分页列表，需要传入 secKillSessionId")
    public R getListBySecKillSessionId(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size, @RequestParam("secKillSessionId") Long secKillSessionId) {

        Page<PromotionAndPromotionSession> page = seckillPromotionService.getListBySecKillSessionId(currentPage, size, secKillSessionId);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取SeckillPromotion")
    public R info(@PathVariable("id") Long id) {

        SeckillPromotionEntity seckillPromotion = seckillPromotionService.getById(id);

        return R.ok().put("seckillPromotion", seckillPromotion);
    }

    @PostMapping("/save")
    @ApiOperation("插入SeckillPromotion")
    public R save(@RequestBody @Validated(SaveGroup.class) SeckillPromotionEntity seckillPromotion) {

        seckillPromotionService.save(seckillPromotion);

        return R.ok();
    }

    @PostMapping("/saveBatch")
    @ApiOperation("批量插入SeckillPromotion")
    @Transactional
    public R saveBatch(@RequestBody  @Validated(SaveGroup.class) List<SeckillPromotionEntity> seckillPromotionList) {
        seckillPromotionService.saveBatch(seckillPromotionList);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新SeckillPromotion")
    public R update(@RequestBody SeckillPromotionEntity seckillPromotion) {
        seckillPromotionService.updateById(seckillPromotion);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除SeckillPromotion")
    public R delete(@RequestBody Long[] ids) {
        seckillPromotionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
